﻿using System;
using System.Collections.Generic;
using System.IO;

using Acl.Data.Mappings;
using Acl.Dynamic;

namespace Acl.Excel
{
    /// <summary>
    /// Excel 导出生成器
    /// </summary>
    public sealed class ExcelExportBuilder : BooleanDisposable
    {
        private readonly Workbook _workbook;
        private ColumnMappingBuilder _mappingBuilder;
        private string _saveAsFile;
        //private bool _needSave;

        /// <summary>
        ///
        /// </summary>
        /// <param name="excelFile"></param>
        /// <exception cref="FileNotFoundException"></exception>
        public ExcelExportBuilder(string excelFile)
        {
            Guard.NotNullOrEmpty(excelFile, nameof(excelFile));
            _workbook = new Workbook(excelFile);
        }

        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="handler"></param>
        /// <returns></returns>
        public ExcelExportBuilder ColumnNameMapping<T>(Action<IColumnNameMappingBuilder<T>> handler)
        {
            _mappingBuilder = ColumnMappingBuilder.ColumnNameMapping(handler);

            return this;
        }

        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="handler"></param>
        /// <returns></returns>
        public ExcelExportBuilder ColumnIndexMapping<T>(Action<IColumnIndexMappingBuilder<T>> handler)
        {
            _mappingBuilder = ColumnMappingBuilder.ColumnIndexMapping(handler);

            return this;
        }

        /// <summary>
        /// 另存
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public ExcelExportBuilder ExportAs(string fileName)
        {
            Guard.NotNullOrEmpty(fileName, "fileName");
            _saveAsFile = fileName;
            return this;
        }

        /// <summary>
        /// 向sheet 中写入内容，如果append = true,是追加内容，否则是覆盖内容,默认覆盖原有内容
        /// </summary>
        /// <param name="entitySet"></param>
        /// <param name="sheetName"></param>
        /// <param name="append"></param>
        /// <returns></returns>
        public ExcelExportBuilder Export(DynamicEntitySet entitySet, string sheetName = null, bool append = false)
        {
            _workbook.Write(entitySet, sheetName, append);
            return this;
        }

        /// <summary>
        /// 向工作薄写入内容，如果append = true,是追加内容，否则是覆盖内容,默认覆盖原有内容
        /// </summary>
        /// <param name="entitySets"></param>
        /// <param name="append"></param>
        public ExcelExportBuilder Export(DynamicEntitySetCollection entitySets, bool append = false)
        {
            _workbook.Write(entitySets, append);
            return this;
        }

        /// <summary>
        /// 向工作薄写入内容，覆盖工作薄中原有内容
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="models"></param>
        /// <param name="sheetName"></param>
        /// <param name="append"></param>
        public ExcelExportBuilder Export<TModel>(
            IEnumerable<TModel> models,
            string sheetName = null,
            bool append = false)
        {
            _workbook.Write(models, sheetName, _mappingBuilder, append);
            return this;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (!string.IsNullOrEmpty(_saveAsFile))
                {
                    _workbook.SaveAs(_saveAsFile);
                }
                //else if(_needSave)
                //{
                //    _workbook.Save();
                //}
            }

            base.Dispose(disposing);
        }
    }
}